package com.taotao.util;

import java.io.IOException;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class FastDFSClient {

	private TrackerClient trackerClient;
	private TrackerServer trackerServer;
	private StorageServer storagServer;
	private StorageClient1 storageClient;

	public FastDFSClient(String conf) throws IOException, MyException {
		if (conf.contains("classpath:")) {
			conf = conf.replace("classpath:", this.getClass().getResource("/")
					.getPath());
		}
		ClientGlobal.init(conf);
		trackerClient = new TrackerClient();
		trackerServer = trackerClient.getConnection();
		storagServer = null;
		storageClient = new StorageClient1(trackerServer, storagServer);
	}

	public String uploadFile(String imgPath, String imgType,
			NameValuePair[] meta) throws IOException, MyException {
		return storageClient.upload_file1(imgPath, imgType, meta);
	}

	public String uploadFile(String imgPath, String imgType)
			throws IOException, MyException {
		return uploadFile(imgPath, imgType, null);
	}

	public String uploadFile(byte[] imgByte, String imgType,
			NameValuePair[] meta) throws IOException, MyException {
		return storageClient.upload_file1(imgByte, imgType, meta);
	}

	public String uploadFile(byte[] imgByte, String imgType)
			throws IOException, MyException {
		return uploadFile(imgByte, imgType, null);
	}

	public int removeFile(String groupName, String storageFileName)
			throws IOException, MyException {
		return storageClient.delete_file(groupName, storageFileName);
	}

	public int removeFile(String storageFileName) throws IOException,
			MyException {
		return storageClient.delete_file("group1", storageFileName);
	}

	public FileInfo findFile(String groupName, String storageFileName)
			throws IOException, MyException {
		return storageClient.query_file_info(groupName, storageFileName);
	}

	public FileInfo findFile(String storageFileName) throws IOException,
			MyException {
		return storageClient.query_file_info("group1", storageFileName);
	}
}
